import Https from "../../utils/api"
const http = new Https
Page({
  data: {
    // 下拉菜单
    first: '区域',
    second: '售价',
    thirds: '房型',
    fours: '筛选',
    _num: 0,
    _res: 0,
    // 筛选
    louceng: [{ name: '低楼层' }, { name: '中楼层' }, { name: '高楼层' }],
    one: 0,
    two: 0,
    third: -1,
    four: 0,
    five: 0,
    six: -1,
    seven: 0,
    isShow: true,
    currentTab: 0,
    provinces: [],
    regions: [],
    prices: [],
    types: [],
    forwards: [],
    fits: [],
    options: [],
    minArea: '',
    maxArea: ''
  },

  onLoad: function (options) {
    http.request('secondHouse/search/price').then(res => {
      this.setData({
        prices: res.data
      })
    })
    http.request('secondHouse/search/type').then(res => {
      this.setData({
        types: res.data
      })
    })
    http.request('secondHouse/search/forward').then(res => {
      this.setData({
        forwards: res.data
      })
    })
    http.request('secondHouse/search/fit').then(res => {
      this.setData({
        fits: res.data
      })
    })
    http.request('city/regions', {
      province: wx.getStorageSync('province')
    }).then(res => {
      console.log(res)
      this.setData({
        regions: res.data
      })
    })
    http.request('city/provinces').then(res => {
      console.log(res)
      var data = res.data
      var arr = []
      data.forEach(element => {
        var info = {
          ...element,
          selected: false
        }
        arr.push(info)
      });
      console.log(arr)
      this.setData({
        provinces: arr
      })
    })
  },


  selected: function (e) {
    var data = this.data.provinces
    console.log(e)
    var id = e.currentTarget.dataset.id;
    data.forEach(element => {
      if (element.id == id) {
        element.selected = true;
      } else {
        element.selected = false;
      }
    });
    http.request('city/regions', {
      province: id
    }).then(res => {
      console.log(res)
      this.setData({
        regions: res.data
      })
    })
    this.setData({
      provinces: data
    })
  },

  // 下拉切换
  hideNav: function () {
    this.setData({
      displays: "none"
    })
  },
  // 区域
  tabNav: function (e) {
    this.setData({
      displays: "block"
    })
    this.setData({
      selected1: false,
      selected2: false,
      selected: true
    })
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {

      var showMode = e.target.dataset.current == 0;

      this.setData({
        currentTab: e.target.dataset.current,
        isShow: showMode
      })
    }
  },
  // 下拉切换中的切换
  // 区域

  // 下拉菜单1 2 3 4
  // 区域
  clickSum: function (e) {
    console.log(e.target.dataset.num)
    this.setData({
      _sum: e.target.dataset.num,
      first: e.target.dataset.name,
      displays: "none"
    })

  },

  clickMum: function (e) {
    console.log(e.target.dataset.num)
    this.setData({
      _mum: e.target.dataset.num
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    console.log(text)
  },

  clickCum: function (e) {
    console.log(e.target.dataset.num)
    this.setData({
      _cum: e.target.dataset.num
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    console.log(text)
  },

  // 售价
  clickNum: function (e) {
    console.log(e.target.dataset.num)
    this.setData({
      _num: e.target.dataset.num
    })
    this.setData({
      second: e.target.dataset.name
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    console.log(text)
  },

  // 房型
  clickHouse: function (e) {
    console.log(e.target.dataset.num)
    this.setData({
      _res: e.target.dataset.num
    })
    this.setData({
      thirds: e.target.dataset.name
    })
    this.setData({
      displays: "none"
    })
  },


  // 筛选
  choseTxtColor: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    console.log(e.currentTarget.dataset.id)
    this.setData({
      one: id
    })
  },
  chaoxiang: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    console.log(id)
    this.setData({
      two: id
    })
  },
  louceng: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      third: id
    })
  },
  zhuangxiu: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      four: id
    })
  },
  quyu: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    console.log(id)
    this.setData({
      five: id
    })
  },
  jiage: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      six: id
    })
  },
  fangxing: function (e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    this.setData({
      seven: id
    })
  },

  queren() {
    var data = this.data.options
    var two = this.data.two;
    data.push({
      forward: two
    })
    var six = this.data.six
    if (six == 0) {
      data.push({
        price: ['<', 100000]
      })
    } else if (six == 1) {
      data.push({
        price: ['<>', '10000', 500000]
      })
    } else if (six == 2) {
      data.push({
        price: ['>', 500000]
      })
    } else {
      data.push({
        price: []
      })
    }
    var four = this.data.four;
    data.push({
      fit_up: four
    })
    var five = this.data.five;
    data.push({
      region: five
    })
    var third = this.data.third;
    if (third == 0) {
      data.push({
        floor: [1, 2, 3, 4, 5]
      })
    } else if (third == 1) {
      data.push({
        floor: [6, 7, 8, 9, 10]
      })
    } else if (third == 2) {
      data.push({
        floor: [11, 12, 13, 14, 15]
      })
    } else {
      data.push({
        floor: []
      })
    }
    var seven = this.data.seven;
    data.push({
      house_type: seven
    })
    data.push({
      minArea: this.data.minArea,
      maxArea: this.data.maxArea
    })

    console.log(data)
    http.request('secondHouse/search/fang', {
      data: data
    }).then(res => {
      console.log(res)
    })
  },

  minArea(e) {
    var value = e.detail.value
    this.setData({
      minArea: value
    })
  },

  maxArea(e) {
    var value = e.detail.value
    this.setData({
      maxArea: value
    })
  },

  change(e){
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
    var data = this.data.provinces
    data.forEach(element => {
       if(element.id == id){
         element.selected = true
       }
    });
    this.setData({
      provinces:data
    })
    http.request('city/regions', {
      province: id
    }).then(res => {
      console.log(res)
      this.setData({
        regions: res.data
      })
    })
  }
})
